//
//  BKMyOrderReuanGoogController.m
//  bestkeep
//
//  Created by mac on 16/12/18.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderReuanGoogController.h"
#import "HeadView.h"
#import "BKMyOrderBackGoodsCell.h"
#import "BKMyOrderWebViewController.h"
#import "BKMyOrderReturnGoodsModel.h"
#import "BKMyOrderReturnGoodsProgressController.h"
#import "BKMyOrderApplyToReturnGoodsControllerViewController.h"


#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"

@interface BKMyOrderReuanGoogController ()
@property (nonatomic,strong)NSString* pape_no;//页数
@property(nonatomic,strong)NSString *total;
@property (nonatomic,strong)NSMutableArray *arr;
@property (nonatomic,strong)NSMutableArray *muArr;
@property (nonatomic , strong)NSArray *dataArr;

@property(nonatomic,strong) ExceptionView *exceptionView;

@property(nonatomic,strong) UIView * view_back;

@end

@implementation BKMyOrderReuanGoogController
-(NSMutableArray *)muArr{
    if (!_muArr ) {
        _muArr = [NSMutableArray array];
        
    }return _muArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"退换货商品列表";
     self.view.backgroundColor =RGB(234, 234, 234);
    _pape_no =@"1";
    [self initView];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
     [self data];
  }
-(void)initView{
    
//    _view_back=[[UIView alloc]init];
    
    
    
    
    
    
    
    
    _bottomView =[UIView new];
    _bottomView.backgroundColor = kOrderBKGreen;
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.equalTo(@(50));
    }];
    _applytableview =[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _applytableview.delegate = self;
    _applytableview.dataSource = self;
    _applytableview.sectionFooterHeight = 5;
   
    
    _applytableview.separatorColor = [UIColor whiteColor];
   _applytableview.backgroundColor =ThemeBackgroundColor;
     [_applytableview registerClass:[BKMyOrderBackGoodsCell class] forCellReuseIdentifier:@"backcell"];
    [self.view addSubview:_applytableview];
    [_applytableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(10);
        make.bottom.equalTo(_bottomView.mas_top).offset(0);
    }];
    UIView *lien =[UIView new];
    lien.backgroundColor =kOrderWhite;
    [_bottomView addSubview:lien];
    [lien mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(0.5));
        make.centerY.equalTo(_bottomView.mas_centerY);
        make.centerX.equalTo(_bottomView.mas_centerX);
        make.height.equalTo(@(50));
    }];
    _titleBtn =[UIButton new];
    [_titleBtn setTitle:@"退换货进度查询" forState:(UIControlStateNormal)];
    [_titleBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_titleBtn addTarget:self action:@selector(titleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_titleBtn];
    [_titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView.mas_left);
        make.top.equalTo(_bottomView.mas_top);
        make.bottom.equalTo(_bottomView.mas_bottom);
        make.right.equalTo(lien.mas_left);
    }];
    _ingApply = [UIButton new];
    [_ingApply setTitle:@"退换货说明" forState:(UIControlStateNormal)];
    [_ingApply setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_ingApply addTarget:self action:@selector(ingApplyAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_ingApply];
    [_ingApply mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(lien.mas_right);
        make.right.equalTo(_bottomView.mas_right);
        make.top.equalTo(_bottomView.mas_top);
        make.bottom.equalTo(_bottomView.mas_bottom);
    }];
    
    [self setupRefresh];
}

#pragma mark 退换货接口

-(void)data{

    
    if (self.exceptionView) {
        [self.exceptionView dismiss];
        self.exceptionView = nil;
    }
    [BKIndicationView showInView:self.view];
      [BKMyOrderGetDataFromNetWork GetOrderReturnGoodsPagesize:@"20" pageno:self.pape_no Success:^(NSArray *Model) {
              [BKIndicationView dismiss];
        [self.applytableview.mj_header endRefreshing];
        [self.applytableview.mj_footer endRefreshing];
          [self.muArr removeAllObjects];
       [self.muArr addObjectsFromArray:Model];
       [self.applytableview reloadData];
     
       if (self.muArr.count ==0) {
        
//           [BKMyOrderExceptionView showInview:self.applytableview andStatus:BKMyOrderStatusUnverse title:nil detailedTitle:@"暂无退换货商品" handler:^(BKMyOrderExceptionView *action) {
//               [self data];
//               [BKMyOrderExceptionView dismiss];
//           }];
           
           self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
           self.exceptionView.contentLabel.text=@"";
           ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoData handler:^(ExceptionViewAction *action) {
               [self.exceptionView dismiss];
               self.exceptionView = nil;
               [self data];
           }];
           [self.exceptionView addAction:action];
           [self.exceptionView showInView:_applytableview];
           
           
       }
       
   } failure:^(NSError *error) {
     
        [BKIndicationView dismiss];
       [self.applytableview.mj_header endRefreshing];
       [self.applytableview.mj_footer endRefreshing];

       
       ExceptionCodeType errorType;
       if (error.code == -1009) {
           errorType = ExceptionCodeTypeNoNetwork;
       }else if (error.code == -1001){
           errorType = ExceptionCodeTypeRequestFailed;
       }else if (error.code == 502 || error.code == 404){
           errorType = ExceptionCodeTypeServerError;
       }else{
           errorType = ExceptionCodeTypeFailedToLoad;
       }
       
       self.exceptionView = [ExceptionView exceptionViewWithType:errorType];
       ExceptionViewAction *action = [ExceptionViewAction actionWithType:errorType handler:^(ExceptionViewAction *action) {
           [self.exceptionView dismiss];
           self.exceptionView = nil;
           [self data];
       }];
       
       [self.exceptionView addAction:action];
       [self.exceptionView showInView:_applytableview];
       
   } currentPage:^(NSDictionary *Page) {
       self.total = Page[@"total"];
       self.pape_no = Page[@"pageno"];
       if (self.muArr.count>0&& (self.muArr.count < [self.total integerValue])) {
           self.applytableview.mj_footer.hidden  = NO;
       }else{
           self.applytableview.mj_footer.hidden  = YES;
       }

   }];
    
}

-(void)setupRefresh{
    
    BKRefreshGifHeader *headerRefresh = [BKRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.applytableview.mj_header = headerRefresh;
   BKMyOrderFooterAutoReferesh *footer = [BKMyOrderFooterAutoReferesh footerWithRefreshingTarget:self refreshingAction:@selector(updataMore)];
    self.applytableview.mj_footer = footer;
    self.applytableview.mj_footer.hidden = YES;
    
}
-(void)refreshData{
    [self.muArr removeAllObjects];
  
    self.pape_no = @"1";
    
    [self data];

}
-(void)updataMore{
   
    NSInteger page = [self.pape_no integerValue] +1;
    self.pape_no = [NSString stringWithFormat:@"%ld",(long)page];
    [self data];
    
    
  }

//退换货进度查询
-(void)titleBtnAction:(UIButton *)button{
    BKMyOrderReturnGoodsProgressController*queryVC = [[BKMyOrderReturnGoodsProgressController alloc]init];
    [self.navigationController pushViewController:queryVC animated:YES];
}
//退换货说明
-(void)ingApplyAction:(UIButton *)btn{
    NSString *reqset =@"http://cdn.bestkeep.cn/ui/pc/agreement/returngoods-agreement.html";
    BKMyOrderWebViewController *ucWK = [[BKMyOrderWebViewController alloc] init];
    ucWK.webViewUrl = reqset;
    ucWK.webViewTitle =@"退换货说明";
    [self.navigationController pushViewController:ucWK animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.muArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BKMyOrderReturnGoodsModel *model = self.muArr[section];
     return model.order_list.count;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  BKMyOrderReturnGoodsModel *model = self.muArr[section];
    HeadView *headView = [[HeadView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    headView.backgroundColor = [UIColor whiteColor];
    headView.model = model;
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 85;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 110;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{BKMyOrderReturnGoodsModel *model = self.muArr[indexPath.section];
   
         BKMyOrderBackGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"backcell"forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model =model.order_list[indexPath.row];
        [cell.backButton addTarget: self  action:@selector(action_btn:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
        
    
}
#pragma  mark---申请退换货   点击事件
-(void)action_btn:(UIButton *)button{
    
    BKMyOrderBackGoodsCell *cell;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ) {
        cell =(BKMyOrderBackGoodsCell *)[button superview].nextResponder;
        
    }else {
        cell = (BKMyOrderBackGoodsCell*)button.superview.superview.superview;
        
    }
    
    BKMyOrderApplyToReturnGoodsControllerViewController *applyVC = [[BKMyOrderApplyToReturnGoodsControllerViewController alloc]init];
    order_list *model = cell.model;
    BK_My_Order_rows_itemList_Model *detailModel = [[BK_My_Order_rows_itemList_Model alloc]init];
      detailModel.goods_img = model.goods_img;
    detailModel.goods_name = model.goods_name;
    detailModel.amount =model.goods_amount;
    detailModel.order_item_id = model.itemId;
   
     NSString *salepropoty = @"";
    for (propList *propModel in model.propList) {
        NSString *str = [NSString stringWithFormat:@"%@:",propModel.name];
        NSString *str1 = [NSString stringWithFormat:@"%@%@",str,propModel.value];
        salepropoty = [NSString stringWithFormat:@"%@%@",salepropoty,str1];
    }
    
    detailModel.sale_property = salepropoty;
    
    applyVC.OrderModel = detailModel;
    
    [self.navigationController pushViewController:applyVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
    
}

@end
